package middlewares

import (
	"ginfwk/infrastructure/facede"
	"github.com/gin-gonic/gin"
	"math/rand"
	"strconv"
)

func Tracer() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		// 获取traceId
		traceId := strconv.Itoa(int(rand.Int63()))
		spanId := strconv.Itoa(int(rand.Int63()))

		// 注入到请求context中
		ctx.Request = ctx.Request.WithContext(
			facede.NewLoggerContext(ctx, traceId, spanId),
		)
		ctx.Next()
	}
}
